package com.surpassun.emall.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.surpassun.emall.model.Permission;
import com.surpassun.emall.model.Role;
import com.surpassun.emall.model.User;
import com.surpassun.emall.repository.PermissionRepository;
import com.surpassun.emall.repository.UserRepository;
import com.surpassun.emall.service.PermissionService;
import com.surpassun.emall.util.Constants;
import com.surpassun.emall.util.QueryUtil;
import com.surpassun.emall.util.Validator;

/**
 *
 * @author Denis
 */
@Service
public class PermissionServiceImpl extends AbstractServiceImpl<Permission> implements PermissionService {

	private Logger _log = LoggerFactory.getLogger(PermissionServiceImpl.class);
	
	@Inject
	PermissionRepository permissionRepository;
	
	@Inject
	UserRepository userRepository;
	
    public PermissionServiceImpl() {
    	super(Permission.class);
    }

	@Override
	public void addPermission(String name, String action) {
		if (Validator.isNotNull(name) && Validator.isNotNull(action)) {
			Permission permission = new Permission(name, action);
			permissionRepository.add(permission);
		}
	}

	@Override
	public void updatePermission(long permissionId, String name, String action) {
		Permission permission = permissionRepository.get(permissionId);
		if (Validator.isNotNull(permission)) {
			permission.setName(name);
			permission.setAction(action);
			super.update(permission);
		}
	}

	@Override
	public boolean hasUserPermission(long userId, String action) {
		User user = userRepository.get(userId);
		if (Validator.isNotNull(user)) {
			List<Role> roles = user.getRoles();
			if (Validator.isNotNull(roles)) {
				for (Role role : roles) {
					//If one of the roles of the user has the permission, the method will return true
					if (hasRolePermission(role, action)) {
						return true;
					}
				}
			}
		}
		
		return false;
	}

	@Override
	public boolean hasRolePermission(Role role, String action) {
		if (Validator.isNotNull(role)) {
			List<Permission> permissions = role.getPermissions();
			if (Validator.isNotNull(permissions)) {
				for (Permission permission : permissions) {
					//If one of the permissions of the role has the action, the method will return true
					if (permission.getAction().equalsIgnoreCase(action)) {
						return true;
					}
				}
			}
		}
		
		return false;
	}

	@Override
	public void initializePermissions() {
		List<String> actions = new ArrayList<String>();
		actions.add(Constants.PERMISSION_ACTION_ACCESS_ADMIN_PAGE);
		actions.add(Constants.PERMISSION_ACTION_ADD_STORE);
		actions.add(Constants.PERMISSION_ACTION_ADD_USER);
		actions.add(Constants.PERMISSION_ACTION_DELETE_STORE);
		actions.add(Constants.PERMISSION_ACTION_DELETE_USER);
		actions.add(Constants.PERMISSION_ACTION_UPDATE_STORE);
		actions.add(Constants.PERMISSION_ACTION_UPDATE_USER);
		addPermissionIfNotExist(actions);
	}
	
	@Override
	public void addPermissionIfNotExist(List<String> actions) {
		for (String action : actions) {
			Permission accessAdminPage = permissionRepository.getSingleByField("name", action, QueryUtil.EQUAL);
			if (Validator.isNull(accessAdminPage)) {
				Permission permission = new Permission(action, action);
				permissionRepository.add(permission);
				_log.info("New permission '" + action + "' added");
			}
		}
	}
    
}